<?php
/**
 * Created by PhpStorm.
 * User: kkw
 * Date: 2019/1/29
 * Time: 15:36
 */
namespace kkw\goods\response;
use kkw\user\table\UserTable;
use kkw\user\Util;
use kkw\goods\table\GoodsImgTable;
use kkw\goods\table\GoodsTable;
use suda\core\Request;
use suda\core\Response;


class GoodsDetailResponse extends  Response
{
    public function onRequest(Request $request)
    {
        Util::printHeader();
        $goods_table = new GoodsTable();
        $json = Request::json();
        $id = $json['id']; //商品的id
        debug()->info(__('get id $0', $id));
        try {
            $goodsDetail = $goods_table ->select('*',['id' => $id]) ->fetch();
            $image = new GoodsImgTable();
            $images = $image->listWhere(['goods_id' => $id]);
            $user = new UserTable();
            $userdata = $user ->select('*',['id'=>$goodsDetail['user_id'] ])->fetch();
        } catch (\suda\exception\SQLException $e) {
            $this->json(['code'=> $e->getCode(), 'message'=>'查询数据出错，请重试']);
            return;
        }
        $this->json([
            'code'=> 0,
            'detail'=> $goodsDetail,
            'image' => $images,
            'userdata'=> $userdata,
            'message'=> '查询数据成功'
        ]);
    }
}